﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Internet_Cafe.App_Code.DataBaseAccess;
using Internet_Cafe.App_Code.DatabaseModel;

namespace Internet_Cafe
{
    /// <summary>
    /// Interaction logic for AddNewPC.xaml
    /// </summary>
    public partial class AddNewPC : Window
    {
        MainWindow mainWinRef;
        public AddNewPC(Window mainWinRef)
        {
            InitializeComponent();
            this.mainWinRef = (MainWindow)mainWinRef;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnAddPC_Click(object sender, RoutedEventArgs e)
        {
            Computer newComputer = new Computer();
            if (System.Text.RegularExpressions.Regex.IsMatch(txtIP.Text, "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"))
            {
                newComputer.IPAddress = txtIP.Text;
                ComputerItem newComputerItem = new ComputerItem(newComputer.IPAddress,newComputer.ID);
                App_Code.DataBaseAccess.DbComputersAccess.Insert(newComputer);
                mainWinRef.lstComputers.Add(newComputer);
                mainWinRef.canvasComputers.Children.Add(newComputerItem);
                if (mainWinRef.menuEdit.IsChecked == true)
                {
                    mainWinRef.unlockItems();
                }
                else if (mainWinRef.menuEdit.IsChecked == false)
                {
                    mainWinRef.lockItems();
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Неправилно внесена IP адреса, обидете се повторно!\nПравилен формат: xxx.xxx.xxx.xxx", "Грешка при внес на IP", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
